﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace mpsalaryprojectgroup
{
    public partial class NEWMainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Clear();
        }

        protected void tbxSignUp_Click(object sender, EventArgs e)
        {
            //sign up
            Session["Usename"] = tbxName.Text;
            Session["Email"] = tbxEmailAddress.Text;
            Session["PasswordSignUp"] = tbxSignUpPassword.Text;

            mpsalary.App_Code.User u = new mpsalary.App_Code.User();
  
            u.CheckEmail();      
            u.CheckUsername();

            
            
            if (u.Usename == tbxName.Text)
            {
                lblerror1.Text = "Username is not available.";
            }
            else if (u.Email == tbxEmailAddress.Text)
            {
                lblerror1.Text = "This email had already been used.";

            }
            else if (u.Email == tbxEmailAddress.Text && u.Usename == tbxName.Text)
            {
                lblerror.Text = " Username is not available.";
            }
            else
            {
                u.Usename = tbxName.Text;
                u.Email = tbxEmailAddress.Text;
                u.Password = tbxSignUpPassword.Text;
                u.CreateUser();


                try
                {
                    MailMessage email = new MailMessage();
                    email.From = new MailAddress("Fromthepgsteam@gmail.com", "PGS Portal");
                    email.To.Add(new MailAddress(tbxEmailAddress.Text));
                    email.Subject = "Welcome to PGS Portal";
                    email.Body = "<html><body><b>Dear " + tbxName.Text + " ,</b> <br><br>You have signed up for an account at PGS Portal successfully.<br><br>Your new username is <b>" + tbxName.Text + "</b>. From now on you will receive PGS Portal notifications through this email account.<br><br>With an account set up, it will be easier then ever to check the yearly wages of Polytechnics Graduates.<br><br><br><b>Yours sincerely,<br> The PGS Team<br><br> Note:</b> If the account has been created without your authorisation, please email thepgsteam@gmail.com to report immediately.</body></html>";
                    email.IsBodyHtml = true;
                    email.Priority = MailPriority.Normal;

                    SmtpClient mailclient = new SmtpClient("smtp.gmail.com");
                    mailclient.Credentials = new System.Net.NetworkCredential("thepgsteam@gmail.com", "thepgsteam");
                    mailclient.Port = 587;
                    mailclient.EnableSsl = true;
                    mailclient.Send(email);

                    // lblemail.Text = "email sent";
                }
                catch
                {
                    // lblemail.Text = "Sending of mail failed";
                }


                Response.Redirect("NEWNEWSignup.aspx");
            }

            
        }

        protected void tbxSignIn_Click(object sender, EventArgs e)
        {
            //log in
            Session["UsenameLogin"] = tbxUsername.Text;
            Session["PasswordLogin"] = tbxNPassword.Text;
            mpsalary.App_Code.User u = new mpsalary.App_Code.User();

            u.Usename = tbxUsername.Text;
            //u.Password = tbxNPassword.Text;

            if (tbxUsername.Text == "admin" && tbxNPassword.Text == "admin")
            {

                Response.Redirect("NEWAdminUploadResult.aspx");

            }
            else if (tbxUsername.Text != "admin" && tbxNPassword.Text != "admin")
            {
                u.RetrievePassword();
                if (tbxNPassword.Text == u.Password)
                {
                    Response.Redirect("NEWUserExportFile.aspx");
                }
                else
                {
                    lblerror.Text = "Sorry, you entered the wrong username/password";
                }

            }
            else
            {
                lblerror.Text = "Sorry, you entered the wrong username and password";
            }
  
        }
    }
}